package com.example.bootdemo.controller;

import com.example.bootdemo.entity.Test;
import com.example.bootdemo.service.TestService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Tag(name = "测试接口", description = "用于测试的一些接口")
@RestController
@RequestMapping("/testapi")
public class TestController {

    private final TestService testService;

    public final StringRedisTemplate stringRedisTemplate;

    private final RedisTemplate<String, Object> redisTemplate;

    public TestController(TestService testService
            , StringRedisTemplate stringRedisTemplate,
                          RedisTemplate<String, Object> redisTemplate) {
        this.testService = testService;
        this.stringRedisTemplate = stringRedisTemplate;
        this.redisTemplate = redisTemplate;
    }

    @Operation(summary = "一个简单的测试接口", description = "返回入参和当前时间")
    @GetMapping("/test")
    public String test(String aa){
        return "test: " + aa + "   " + new Date();
    }

    @Operation(summary = "获取数据表数据", description = "获取测试表的全部数据")
    @GetMapping("/get/users")
    public List<Test> getAllUsers() {
        return testService.getAllUsers();
    }

    @Operation(summary = "获取数据表数据", description = "获取测试表的全部数据")
    @GetMapping("/get/a/object")
    public Test getATestObject() {
        Test test = new Test();
        test.setAge(18);
        test.setName("张三");
        return test;
    }

    @Operation(summary = "redis操作字符串（String）")
    @GetMapping("/redis/get/string")
    public String redisGetString() {
        // 使用StringRedisTemplate（键值均为String类型）
        stringRedisTemplate.opsForValue().set("greeting", "Hello, Redis!");
        String value = stringRedisTemplate.opsForValue().get("greeting");
        return value;
    }

    @Operation(summary = "redis操作哈希（Hash）")
    @GetMapping("/redis/oper/hash")
    public String redisOperHash() {
        // 存储哈希
        stringRedisTemplate.opsForHash().put("user:1000", "name", "Alice");
        stringRedisTemplate.opsForHash().put("user:1000", "age", "30");

        // 获取哈希字段
        String name = (String) stringRedisTemplate.opsForHash().get("user:1000", "name");
        return name;
    }

    @Operation(summary = "redis操作列表（List）")
    @GetMapping("/redis/oper/list")
    public List<String> redisOperList() {
        // 存储列表
        stringRedisTemplate.opsForList().leftPush("mylist", "apple");
        stringRedisTemplate.opsForList().leftPush("mylist", "banana");
        stringRedisTemplate.opsForList().leftPush("mylist", "cherry");

        // 获取列表
        List<String> list = stringRedisTemplate.opsForList().range("mylist", 0, -1);
        return list;
    }

    @Operation(summary = "redis操作对象（使用RedisTemplate）")
    @GetMapping("/redis/oper/object")
    public Test redisOperObject() {
        // 存储对象
        Test test = new Test();
        test.setAge(18);
        test.setName("张三");
        redisTemplate.opsForValue().set("test", test);

        // 获取对象
        Test test1 = (Test) redisTemplate.opsForValue().get("test");
        return test1;
    }

}
